package com.lbjhan.rpc.handler;

import com.lbjhan.rpc.pojo.SerializerOption;
import com.lbjhan.rpc.serializer.ISerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcCommonEncoder extends MessageToByteEncoder {
    private ISerializer serializer;
    private int classOption;
    private int serializerOption;
    public RpcCommonEncoder(ISerializer serializer, int classOption, int serializerOption){
        this.serializer = serializer;
        this.classOption = classOption;
        this.serializerOption = serializerOption;
    }
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        byte[] bytes = serializer.serialize(o);
        byteBuf.writeInt(serializerOption);
        byteBuf.writeInt(classOption);
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);
    }
}
